<template>
  <!-- 上传厂家证照的表格modal -->
  <div>
    <basic-modal :modalOpt="modal" @closeModal="closeModal" :footer="footer" @onMakeSure="onMakeSure">
      <template>
        <batch-operat-btn v-if="editable" :isView="false" :addData="false" @onAddData="onAddData">
          <template #cusBtn>
            <div style="display:flex;alignItems:center;">
              <manufacturerSelect
                style="width:250px"
                placeholder="选择生产企业"
                @change="onSelect(arguments)"
                :value="manufacturerLicense">
              </manufacturerSelect>
              <a-popconfirm placement="topLeft" ok-text="是" cancel-text="否" @confirm="onAddManufacturer">
                <template slot="title">
                  <p>是否跳转到生产企业管理页面</p>
                  <p>添加生产企业需跳转到生产企业管理页面</p>
                </template>
                <a-button style="margin:0px 5px" icon="plus-square">添加生产企业</a-button>
              </a-popconfirm>
            </div>
          </template>
        </batch-operat-btn>
        <div class="recordBox">
          <a-list-table
            :columns="columns"
            :tableData="listdata"
            :isSelecttion="false"
            :loading="loading"
            :scroll="{ y: null }"
            @onAction="onAction"
          >
          </a-list-table>
        </div>
      </template>
    </basic-modal>
    <add-seller-lic-child-modal ref="addSellerLicChildModal" @onAddInfo="onAddInfo" />
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import aListTable from '@/views/app/biz/common/aListTable'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'
import addSellerLicChildModal from './addSellerLicChildModal.vue'
import manufacturerSelect from '../manufacturer/manufacturerSelect.vue' 
// import { getInfoByPid as getSupplierInfoByPid } from '@/views/app/biz/supplier/supplier'
import { getLicenseByPid, saveSellerLicense } from './regisLicense'

export default {
  components: {
    basicModal,
    aListTable,
    batchOperatBtn,
    addSellerLicChildModal,
    manufacturerSelect
  },
  data() {
    return {
      footer:undefined,
      editable:true,
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '上传厂家证照',
        modalWidth: '1000px',
      },
      columns: [
        { title: '证照类型', dataIndex: 'zhengJielx', width: 80, key: 'zhengJielx', align: 'center' },
        { title: '证照号', dataIndex: 'zhengJiehm', key: 'zhengJiehm', align: 'center' },
        { title: '证照开始日期', dataIndex: 'youXiaoqs', key: 'youXiaoqs', align: 'center' },
        { title: '证照结束日期', dataIndex: 'youXiaoqz', key: 'youXiaoqz', align: 'center' },
        { title: '证照备注', dataIndex: 'zhengJiebz', key: 'zhengJiebz', align: 'center' },
        {
          title: '证照图片',
          dataIndex: 'zhengJietp',
          key: 'zhengJietp',
          align: 'center',
          scopedSlots: { customRender: 'img' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 100,
          key: 'operation',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      manufacturer:null,
      manufacturerLicense:null,
      listdata: [],
      listKey: 0,
      pid: '',
      sysSeq: 0,
      loadings: false,
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 点击了上传
    addModal(val) {
      this.modal.modalVisible = true
      this.manufacturerLicense=null
      this.modal.modalTitle = '上传厂家证照'
      this.listdata = []
      this.pid = val
      this.getSBypid(val)
    },
    // 点击了查看editabl传true或者未传值表示可编辑，传false表示不可编辑
    editModal(val,editable) {
      console.log(this.columns);
      console.log('点击了查看')
      if(editable==false){
        this.footer=null
        this.editable=false
        // this.columns.pop()
        this.columns.splice(6, 1)
      }
      this.pid = val
      this.getSBypid(val)
      this.modal.modalVisible = true
      this.modal.modalTitle = '查看厂家证照'
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      Object.assign(this.$data,this.$options.data())
      this.$emit('close')
    },
    //
    onAddData() {
      this.$refs.addSellerLicChildModal.addModal(this.pid)
    },
    // 子modal提交过来的信息
    onAddInfo(form) {
      let existed = false
      this.listdata.find((item) => {
        if (form.zhengJiehm == item.zhengJiehm) {
          existed = true
        }
      })
      if (!existed) {
        this.listdata.push({
          key: form.zhengJiehm,
          action: ['删除'],
          ...form,
        })
        this.$refs.addSellerLicChildModal.closeModal()
      } else {
        this.$message.warn({ content: '该证件号已存在' })
      }
      console.log(this.listdata)
    },
    onSelect(val,name){
      console.log(val,name);
      this.manufacturer=val[1]
      this.manufacturerLicense=val[0]
      if (val[1].licenceMaster) {
        this.listdata = JSON.parse(val[1].licenceMaster)
      }else{
        this.listdata=[]
      }
    },
    onAddManufacturer(){
      this.$router.push({ 
        name: 'manufacturerManage', 
        params: {
          isJump:true ,
          pathName:this.$route.name,
        } 
      })
    },
    // 模态详细框提交按钮
    onMakeSure() {
      // if(this.listdata.length){
      let listdata = JSON.stringify(this.listdata)
      let subData = {
        sysSeq: this.sysSeq,
        pid: this.pid,
        shengChancszz: listdata,
        shengChancszzzt: listdata === '[]' ? '0': '1'
      }
      console.log(listdata)
      this.saveObjInfo(subData)
      // }else{
      //   this.$message.warn({ content: '暂未添加任何厂家证照信息', duration: 2 })
      // }
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.$message.loading({ content: '提交中',duration: 2  })
      let res = await saveSellerLicense(val)
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.closeModal()
        this.$emit('onSaveModal')
      } else {
        this.$message.warn({ content: '提交失败', duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      this.loading=true
      let res = await getLicenseByPid(val)
      if (res && res.code && res.code === 'C1') {
        this.sysSeq = res.data.sysSeq
        if (res.data.shengChancszz) {
          this.listdata = JSON.parse(res.data.shengChancszz)
        }else{
          this.listdata=[]
        }
        this.loading=false
      }
      console.log(res)
    },
    onAction(text, record, index) {
      console.log(text, record, index)
      if (text == '删除') {
        this.listdata.splice(index, 1)
      }
    },
  },
}
</script>

<style>
.recordBox {
  width: 100%;
  min-height: 80px;
}
</style>